<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
      <el-breadcrumb-item>分类参数</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图 -->
    <el-card>
      <!-- 搜索添加 -->
      <el-row :gutter="20">
        <el-col :span="10">
          <el-input placeholder="请输入内容" clearable v-model="addFormModel">
            <el-button slot="append" icon="el-icon-search" @click="findModel"></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="addFenlei">添加分类</el-button>
        </el-col>
      </el-row>
      <!-- 分类表格框架 -->
      <tree-table :data="cateList" :columns="columns" :selection-type="false" :expand-type="false" :show-index="true"
        border>
        <template slot="isok" slot-scope="scope">
          <!--自定义模板 ,模板的名称为isok-- 渲染图标i标签 -->
          <i v-if="scope.row.cat_deleted === false" class="el-icon-success" style="color: #ccc;"></i>
          <i v-else class="el-icon-error" style="color: black;"></i>
        </template>
        <!--自定义模板 ,模板的名称为order 分层级渲染 -->
        <template slot="order" slot-scope="scope">
          <el-tag v-if="scope.row.cat_level === 0" size="mini">一级</el-tag>
          <el-tag v-else-if="scope.row.cat_level === 1" type="success">二级</el-tag>
          <el-tag v-else="scope.row.cat_level===2" type="warning">三级</el-tag>
        </template>
        <!-- 自定义模板,模板名称为op,操作按钮 -->
        <template slot="op" slot-scope="scope">
          <el-button type="primary" size="mini">编辑</el-button>
          <el-button type="danger" size="mini">删除</el-button>
        </template>
      </tree-table>
      <!-- 分页页面 -->
      <el-pagination @size-change="handSize" @current-change="handCurrent" :current-page="queryInfo.pagenum"
        :page-sizes="[5, 10, 15, 20]" :page-size="queryInfo.pagesize" layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>


    </el-card>
    <!-- 添加分类对话框 -->
    <el-dialog title="添加分类" :visible.sync="dialogFormVisible" @close="closeDialog">
      <el-form :model="addCateForm" :rules="addFormRules" ref="addFormRef">
        <el-form-item label="分类名称" prop="cat_name">
          <el-input v-model="addCateForm.cat_name"></el-input>
        </el-form-item>
        <el-form-item label="分类等级">
          <!-- options是级联选择器的数据源 -->
          <!-- props是级联选择器的配置对象 -->
          <!-- expandTrigger: 'hover' 鼠标移入展开 -->
          <el-cascader v-model="selectedKeys" :options="addForm" :props="props" placeholder="请选择分类" @change="handleChange"
            clearable>
          </el-cascader>

        </el-form-item>
      </el-form>
      <span slot="footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="addCate">确 定</el-button>
      </span>
    </el-dialog>





  </div>
</template>

<script>
export default {
  name: "WorkspaceJsonCate",
  data () {
    return {
      cateList: [],
      queryInfo: {

        type: 3,
        pagenum: 1,
        pagesize: 10
      },
      //搜索框的数据源
      addFormModel: '',
      //分类对话框显示与隐藏
      dialogFormVisible: false,
      total: 0,
      // table指定列的定义
      columns: [
        { label: "分类名称", prop: "cat_name" },
        {
          label: "是否有效",
          //自定义模板 ,模板的名称为isok
          type: "template", template: "isok"
        },
        {//自定义模板 ,模板的名称为order
          label: "分类等级", prop: "cat_level",
          type: "template", template: "order"
        },
        {//自定义模板 ,模板的名称为op
          label: "操作",
          type: "template",
          template: "op"
        }


      ],
      //级联选择器数据源
      addForm: [],
      //form表单的数据源
      addFormDialog: {
        cat_name: '',
        cat_deleted: false,
      },
      //添加分类参数
      addCateForm: {
        cat_name: '',
        cat_pid: 0,
        cat_level: 0,
      },
      //props配置项
      props: {
        value: "cat_id",
        label: "cat_name",
        children: "children",
        expandTrigger: 'hover',
        checkStrictly: true,
      },
      //选中的父级id数组
      selectedKeys: [],
      //校验规则
      addFormRules: {
        cat_name: [
          { required: true, message: "请输入分类名称", trigger: "blur", message: "请输入分类名称" },
        ]
      },






    };
  },
  mounted () {
  },
  created () {
    this.getCateList();
  },
  methods: {
    // 分类商品数据列表获取
    async getCateList () {
      const { data: res } = await this.$http.get("categories", {
        params: this.queryInfo
      });
      if (res.meta.status !== 200)
        return this.$message.error("获取商品分类列表失败！");
      this.cateList = res.data.result;
      this.total = res.data.total;
    },
    //监听pagesize的变化
    handSize (val) {
      this.queryInfo.pagesize = val;
      this.getCateList();
    },
    // 监听当前页码的变化
    handCurrent (val) {
      this.queryInfo.pagenum = val;
      this.getCateList();
    },
    //选择项发生变化触发
    handleChange () {
      // console.log(this.selectedKeys.length);
      // console.log(this.selectedKeys);
      if(this.selectedKeys.length>0){
          this.addCateForm.cat_pid = this.selectedKeys[this.selectedKeys.length - 1];
          this.addCateForm.cat_level = this.selectedKeys.length;
      }
      else{
        this.addCateForm.cat_pid = 0;
        this.addCateForm.cat_level = 0;
      }
    
    },

    //分类对框显示与隐藏
    addFenlei () {
      this.getFenlei();
      this.dialogFormVisible = true;
    },
    //获取父级分类的数据列表
    async getFenlei () {
      const { data: res } = await this.$http.get("categories", {
        params: {
          type: 2
        }
      });
      // console.log(res);
      if (res.meta.status !== 200)
        return this.$message.error("获取父级分类列表失败！");
      this.addForm = res.data;
    },
    //点击确定提交
    addCate () {
      this.$refs.addFormRef.validate(async valid => {
        if (!valid) return;
        //发送请求
        const { data: res } = await this.$http.post("categories", this.addCateForm);
        // console.log(res);
        if (res.meta.status !== 201) {
          return this.$message.error("添加分类失败！");
        }
        this.$message.success("添加分类成功！");
        this.dialogFormVisible = false;
        this.getCateList();
      });
    },
    //监听对话框关闭事件,充值表单
    closeDialog () {
      this.$refs.addFormRef.resetFields();
      this.selectedKeys = [];
      this.addCateForm.cat_level = 0;
      this.addCateForm.cat_pid = 0;
    },
    //点击搜索按钮,获取输入框的值,进行搜索
    findModel () {
      const a = this.cateList.filter((item) => {
        //如包含输入框的值,则返回
        if (item.cat_name.includes(this.addFormModel)) {
          return item
        }

      })
      //如果没有找到,则message提示弹窗
      if (a.length === 0) {
        this.getCateList();
        this.$message.error("没有找到该分类");
      } else {
        //如果找到了,则把找到的数据赋值给cateList
        this.cateList = a;

      }

    }



  },
  //结尾
}
</script>

<style lang="less" scoped>
.el-row {
  margin-bottom: 20px;
}
</style>